package com.gpoint.rdp.web.shiro;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gpoint.rdp.components.querybuilder.utils.StringUtils;

import cn.pluss.platform.service.AuthRemoteService;

@Controller
@RequestMapping("platform/authRemote")
public class AuthRemoteController {
	
	@Resource
	private AuthRemoteService authRemoteService;
	
    @RequestMapping(value = "/checkTicket", method = RequestMethod.GET)
	public String checkTicket(String ticket,HttpServletResponse response,HttpServletRequest request) {
		try {
			if (StringUtils.isEmpty(ticket)) {
				response.sendRedirect(authRemoteService.getServiceUrl());
				return null;
			}
			UsernamePasswordToken token = new UsernamePasswordToken(ticket, ticket, false);
			SecurityUtils.getSubject().login(token);
			WebUtils.redirectToSavedRequest(request, response, authRemoteService.getServiceUrl());
		} catch (IOException e) {
		}
		return null;
	}
	
}
